/*3. Blogrum a la jQuery
----------------------

You run a site with a blog and a forum, and you want to show some of
your recent blog posts in your forum's header. Unfortunately, you
don't have the ability to add server-side code to your forum software.
Using jQuery, create a script that will fetch your blog's RSS feed and
display links to the three most recent posts in a page element. Also,
include paging functionality to allow the viewer to see the rest of
the posts, three at a time. For the sake of this exercise, assume that
both the blog and the forum are on the same domain.

Submit your implementation as a text file.
*/

/* Was not sure by what was by 'both the blog and the forum are on the same domain'
 * So I made the assumption that this code sits on my website, and that the blog's 
 * RSS feed is the same as the domain name that the blog and forum lives on.
 */
feedURL = document.location.hostname;
    
var xmlURL = "http" + "://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + encodeURIComponent(feedURL);
    
// ID of the <div> to load into
loadID = "#display";

var noName = {
    data: null
    ,postCount : null
    ,postPerPage : 3
    ,currentIndex : 0
    ,pagingIndex : 0
    ,init: function(data) {
        this.data = data;
        this.postCount = data.entries.length;
        if(this.postCount > this.postPerPage) {
            this.pagingIndex = this.postPerPage;
        }
        this.show(0); // show first 3 posts
    }
    ,show: function(index) {
        $(loadID).empty();
        for(i=index; i<this.pagingIndex; i++) {
            this.currentIndex = i;
            var jsonObj = this.data.entries[i];
            if(jsonObj) {
                var title = jsonObj.title;
                var link = jsonObj.link;
                $(loadID).append("<p><a href=\"" + link + "\" target=\"_blank\">" + title + "</a></p>");    
            }
        }
        var next = $("<a>").attr("href","#").click(this.nextHandler).text("next >>");
        var previous = $("<a>").attr("href","#").click(this.previousHandler).text("<< previous");
        
        if(this.pagingIndex + this.postPerPage <= this.postCount) {
            this.pagingIndex += this.postPerPage;
        } else {
            this.pagingIndex = this.postCount;
        }
        $(loadID).append(previous);
        $(loadID).append(document.createTextNode(" "));
        $(loadID).append(next);
    }
    ,nextHandler: function() {
        //disable 'next' link if at the end of the posts
        if(noName.currentIndex+1 == noName.postCount) {
            alert("No more posts to show.");
            return false;
        } else {
            noName.show(noName.currentIndex+1);
        }
    }
    ,previousHandler: function() {
        //disable 'previous' link if at the beginning of the posts
        if(noName.currentIndex - noName.postPerPage >= 0) {
            noName.pagingIndex = noName.currentIndex;
            noName.currentIndex = noName.currentIndex - noName.postPerPage;
            noName.show(noName.currentIndex);
        } else {
            alert("No more posts to show.");
            return false;
        }
    }
};

    function getFeed()
    {
      $.getJSON(xmlURL, function(data){
			if (data.responseStatus == 200) {
                noName.init(data.responseData.feed);
			} else {
				alert("Error loading");
			};
		});  
    };

  $(document).ready(function() {
		getFeed();
	});
    